﻿/*
----------------------------------------
    Rodeo Game Engine

    Rodeo Core Module
    Singleton.h - header file

----------------------------------------   
 
    !IMPORTANT NOTES!
    Source code by Scott Bilas, 
    "Game Programming Gems 1"
 
    HOW TO USE:
    1. Public Inherit Singleton <MyClass> in MyClass
    2. Remember to create object of class MyClass
    3. Call MyClass::GetInstance()
    Example:
         class Enemy : public Singleton <Enemy>
         {
         public:
              Enemy* GetName(const char* name);
         };
         void ShowMeEnemyName()
         {
              Enemy* terrorist = Enemy::GetInstance().GetName("OsamaBinLaden");
              Enemy* monster = Enemy::GetInstance().GetName("Dragon");
         }
-----------------------------------------
*/
#ifndef RODEO_CORE_SINGLETON_H_
#define RODEO_CORE_SINGLETON_H_

namespace rodeocore
{
template <typename T>class Singleton
{
    static T* pSingleton;

public:
    Singleton()
    {
        int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
        pSingleton = (T*)((int)this + offset);
    }
    ~Singleton()
    {
        pSingleton = 0;
    }
    static T& getInstance()
    {
        return (* pSingleton);
    }
    static T* getInstanceByPtr()
    {
        return (pSingleton);
    }
};

template <typename T> T* Singleton <T>::pSingleton = 0;
}    // end of rodeocore namespace
#endif    //RODEO_CORE_SINGLETON_H_